package com.somsoft.unlockme;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.widget.ImageButton;

import com.baidu.mobstat.StatService;
import com.hjgame.tt.PAManager;
import com.somsoft.unlockme.db.Service;
import com.somsoft.unlockme.fk.C;

public class MainActivity extends Activity {
	
	ImageButton btnStartGame;
	ImageButton btnGoOnGame;
	ImageButton btnGameOption;
	ImageButton btnExitGame;
	private DisplayMetrics dm;
	
	
	//主线程的Handler对象
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {//重写处理消息方法
			int what = msg.what;
			if(what == 1){
				//进入游戏界面
				go();
			}
			if(what==2){
				//重新获取关卡
				getLevelInfo();
			}
		}		
	};
	
	private void getLevelInfo(){
		Service service = new Service(this);  
		C.lvs = service.getLevels((C.currPage - 1) * 9, 9);
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		
		PAManager pm = PAManager.getInstance(this);
		pm.setCooId(this, "169c4b77c106468cb366ced1fe8d9c92");
		pm.setChannelId(this, "skhrd-androidmarket");
		pm.receiveMessage(this, true);
		
		
        dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		init();
		getLevelInfo();
		setContentView(new MainView(this));
	}

	private void go(){
		C.currMoves = 0;
		Service service = new Service(this);  
		C.score =  service.find(C.currLevel).getScore();
		Intent it = new Intent(this,UnlockMeActivity.class);
		startActivity(it);
	}
	
	/**
	 * 初始化系统参数（C.java中的参数）
	 */
	private void init(){
		float w = dm.widthPixels / 1.0f / 480;
		float h = dm.heightPixels / 1.0f / 800;
		if (w < h) {
			C.scaleScreen = w;
		} else {
			C.scaleScreen = h;
		}
		C.width = (int) dm.widthPixels;
		C.height = (int) dm.heightPixels;
		C.realWidth = (int) (480 * C.scaleScreen);
		C.realHeight = (int) (800 * C.scaleScreen);
		C.leftKongBai = (C.width - C.realWidth) / 2;
		C.topKongBai = (C.height - C.realHeight) / 2;
		C.startX = C.leftKongBai + (int) (60 * C.scaleScreen);
		C.startY = C.topKongBai + (int) (295 * C.scaleScreen);
		C.scaleBitmap = 1 / dm.density * C.scaleScreen;
		C.unit = (int) (90 * C.scaleScreen);
		C.successX = C.startX + C.unit;
		C.successY = C.startY + C.unit * 3;
		
		//选关界面参数
		C.lockStartX = C.leftKongBai + (int) (40 * C.scaleScreen);
		C.lockStartY = C.topKongBai + (int) (215 * C.scaleScreen);
		C.unitLock = (int) (100 * C.scaleScreen);
		C.unitNumber = (int) (65 * C.scaleScreen);
		C.unitStar = (int) (30 * C.scaleScreen);
		C.unitArrow = (int) (56 * C.scaleScreen);
		C.unitPoint = (int) (36 * C.scaleScreen);
		C.jiange = (int) (25 * C.scaleScreen);
		C.arrowStartY = (int) ((800-180+5) * C.scaleScreen);
		C.rightArrowStartX = (int) ((C.lockStartX+400-C.unitArrow) * C.scaleScreen);
		C.pointJianGe = (int) (18 * C.scaleScreen);
		
		C.fontNumber = Typeface.createFromAsset(getAssets(), "font/Farrington.ttf");
	}
	
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK){
			MyApplication.getInstance().exit();
		}
		return super.onKeyUp(keyCode, event);
	}
	
	public void onResume() {
	    super.onResume();
	    StatService.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    StatService.onPause (this);
	}

}
